import React from 'react';
import {BrowserRouter as Router, Route, withRouter} from "react-router-dom";
import Home from './Home';
import Certification from './Certification';
import CertificationProd from './CertificationProd';

import Antiracketeering from './Antiracketeering'
import ServiceProcess from './ServiceProcess';
import ServiceProcessProd from './ServiceProcessProd';
import ArticleDetail from './ArticleDetail';
import MensesArticleDetail from './MensesArticleDetail';
import UGC from './UGC';
import Share from './Share';
import CRMReview from './CRMReview';
import BuyVac from './MISC/BuyVac';
import BuyVacc from './MISC/BuyVacc';
import BookingRegister from './MISC/BookingRegister';
import BookingRegisterFlag from './MISC/BookingRegisterFlag';
import BookingRegisterBoard from './MISC/BookingRegisterBoard';
import BookingRegisterHK from './MISC/BookingRegisterHK';
import BookingConfirmRegister from './MISC/BookingConfirmRegister';
import BookingConfirmRegisterFlag from './MISC/BookingConfirmRegisterFlag';
import BookingRegisterShangHaiFlag from './MISC/BookingRegisterShangHaiFlag';
import BookingRegisterJingDong from './MISC/JingDong/BookingRegister';
import BookingConfirmRegisterJingDong from './MISC/JingDong/BookingConfirmRegister';
import BookingConfirmRegisterPinDuoDuo from './MISC/PinDuoDuo/BookingConfirmRegister';
import InsuranceAccept from './MISC/InsuranceAccept';
import PreorderHome from './Preorder/Home';
import PreorderSubmitQueue from './Preorder/SubmitQueue';
import PreorderPaymentFail from './Preorder/PaymentFail';
import PreorderDetails from './Preorder/Details';
import VaccineSearch from './VaccineSearch';
import NotFound from './NotFound';
import Loading from 'loading';
import * as Sentry from '@sentry/browser';
import wxjssdk from 'libs/wxjssdk';
import {isWechat} from 'libs/tools';
import ProductDetail from "./MISC/ProductDetail";


if (DEPLOY_ENV === 'prod') {
  Sentry.init({
    dsn: "https://e93c6640beba459aac96f4705edc39df@sentry.io/1403519",
    environment: DEPLOY_ENV
  });
}

//  init wechat jssdk if in wechat environment

@withRouter
class Routes extends React.Component {
  componentDidCatch(error, errorInfo) {
    this.setState({error});
    Sentry.withScope(scope => {
      Object.keys(errorInfo).forEach(key => {
        scope.setExtra(key, errorInfo[key]);
      });
      Sentry.captureException(error);
    });
  }

  componentDidUpdate(prevProps) {
    if (this.props.location !== prevProps.location) {
      this.onRouteChanged();
    }
  }

  onRouteChanged() {
    //  fix wechat jssdk invalid signature
    if (isWechat()) {
      wxjssdk.init();
    }
  }

  render() {
    return (
      <React.Fragment>
        <Route exact path="/" component={Home}/>

        <Route path="/antiracketeering" component={Antiracketeering}/>
        <Route path="/certification" component={Certification}/>
        <Route path="/CertificationProd" component={CertificationProd}/>
        <Route path="/service-process" component={ServiceProcess}/>
        <Route path="/service-processProd" component={ServiceProcessProd}/>
        <Route path="/VaccineSearch" component={VaccineSearch}/>
        <Route path="/preorder/home" component={PreorderHome}/>
        <Route path="/preorder/submit-queue" component={PreorderSubmitQueue}/>
        <Route path="/preorder/payment-fail/:recordId" component={PreorderPaymentFail}/>
        <Route path="/preorder/details/:recordId" component={PreorderDetails}/>
        <Route path="/article/:id" component={ArticleDetail}/>
        <Route path="/menses-article/:id" component={MensesArticleDetail}/>
        <Route path="/ugc/:id" component={UGC}/>
        <Route path="/share" component={Share}/>
        <Route path="/crm-review" component={CRMReview}/>
        <Route path="/misc/productdetail" component={ProductDetail}/>
        <Route path="/misc/app-BuyVac" component={BuyVac}/>
        <Route path="/misc/app-BuyVacc" component={BuyVacc}/>
        <Route path="/misc/booking-register" component={BookingRegister}/>
        <Route path="/misc/booking-register-flag" component={BookingRegisterFlag}/>
        <Route path="/misc/booking-register-shanghai-flag" component={BookingRegisterShangHaiFlag}/>
        <Route path="/misc/booking-register-board" component={BookingRegisterBoard}/>
        <Route path="/misc/booking-register-HK" component={BookingRegisterHK}/>
        <Route path="/misc/booking-confirm-register" component={BookingConfirmRegister}/>
        <Route path="/misc/booking-confirm-register-flag" component={BookingConfirmRegisterFlag}/>
        <Route path="/misc/jingdong/booking-register" component={BookingRegisterJingDong}/>
        <Route path="/misc/jingdong/booking-confirm-register" component={BookingConfirmRegisterJingDong}/>
        <Route path="/misc/pinduoduo/booking-confirm-register" component={BookingConfirmRegisterPinDuoDuo}/>
        <Route path="/misc/insurance-accept" component={InsuranceAccept}/>
        <Route path="/404" component={NotFound}/>
      </React.Fragment>
    )
  }
}

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      error: false
    };
  }

  componentDidCatch(error, errorInfo) {
    this.setState({error});
  }

  render() {
    let {error} = this.state;

    return (
      error ?
        <div className="global-error-tips"></div>
        :
        <React.Fragment>
          <Router basename="/app-web">
            <Routes/>
          </Router>
          <Loading className="global-loading"></Loading>
        </React.Fragment>
    );
  }
}




